﻿using System;
using System.Collections.Generic;
using System.Text;
using DO.Clustering;

namespace BLL.Relations.VectorDistances
{
    /// <summary>
    /// 
    /// </summary>
    public class EuclideanDistanceCalculator : IDistanceCalculator
    {
        #region IDistanceCalculator Members
        /// <summary>
        /// 
        /// </summary>
        /// <param name="FeatureFreq1"></param>
        /// <param name="FeatureFreq2"></param>
        /// <param name="contributingFeatures"></param>
        /// <returns></returns>
        public double CalculateDistance(
            Dictionary<int, FeatureFreq> FeatureFreq1,
            Dictionary<int, FeatureFreq> FeatureFreq2,
            ref Dictionary<int, int> contributingFeatures)
        {
            if (FeatureFreq1 == null || FeatureFreq2 == null || FeatureFreq1.Count == 0 || FeatureFreq2.Count == 0)
                return double.PositiveInfinity;


            List<int> unionFeatureIDs = new List<int>();
            foreach (int FeatureID1 in FeatureFreq1.Keys)
            {
                unionFeatureIDs.Add(FeatureID1);
            }
            foreach (int FeatureID2 in FeatureFreq2.Keys)
            {
                if (!unionFeatureIDs.Contains(FeatureID2))
                {
                    unionFeatureIDs.Add(FeatureID2);
                }
            }
            double sumSq = 0;
            foreach (int FeatureID in unionFeatureIDs)
            {
                if (FeatureFreq1.ContainsKey(FeatureID) && FeatureFreq2.ContainsKey(FeatureID))
                {
                    sumSq += 0;
                    contributingFeatures.Add(FeatureID, Math.Min(FeatureFreq1[FeatureID].Count, FeatureFreq2[FeatureID].Count));
                }
                else
                {
                    int freq1 = 0;
                    if (FeatureFreq1.ContainsKey(FeatureID))
                        freq1 = FeatureFreq1[FeatureID].Count;
                    int freq2 = 0;
                    if (FeatureFreq2.ContainsKey(FeatureID))
                        freq2 = FeatureFreq2[FeatureID].Count;
                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double) contributingFeatures.Count*2/
                                 (FeatureFreq1.Count + FeatureFreq2.Count - contributingFeatures.Count);
            double euclideanDistance = Math.Sqrt(sumSq) * docLenScale;

            double contributingFeatureScale =
                FeatureWeightCalculator.TaminoCommonFeatureScale(
                    FeatureFreq1.Count, FeatureFreq2.Count,
                    contributingFeatures.Count);
            return euclideanDistance*contributingFeatureScale;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="FeatureFreq1"></param>
        /// <param name="FeatureFreq2"></param>
        /// <param name="FeatureWeights"></param>
        /// <param name="useNGramSimilarity"></param>
        /// <param name="contributingFeatures"></param>
        /// <returns></returns>
        public double CalculateDistance(
            Dictionary<int, FeatureFreq> FeatureFreq1,
            Dictionary<int, FeatureFreq> FeatureFreq2,
            Dictionary<int, double> FeatureWeights,
            bool useNGramSimilarity, int ngramLen,
            ref Dictionary<int, int> contributingFeatures)
        {
            if (FeatureFreq1 == null || FeatureFreq2 == null || FeatureFreq1.Count == 0 || FeatureFreq2.Count == 0)
                return double.PositiveInfinity;

            List<int> unionFeatureIDs = new List<int>();
            foreach (int FeatureID1 in FeatureFreq1.Keys)
            {
                unionFeatureIDs.Add(FeatureID1);
            }
            foreach (int FeatureID2 in FeatureFreq2.Keys)
            {
                if (!unionFeatureIDs.Contains(FeatureID2))
                {
                    unionFeatureIDs.Add(FeatureID2);
                }
            }
            double sumSq = 0;
            Dictionary<int, Dictionary<int, double>> featureSimilarityMatrix =
                FeatureSimilarityMatrix.BuildFeatureDistanceMatrixUsingNgram(
                    FeatureFreq1, FeatureFreq2);

            foreach (int FeatureID in unionFeatureIDs)
            {
                if (FeatureFreq1.ContainsKey(FeatureID) && FeatureFreq2.ContainsKey(FeatureID))
                {
                    sumSq += 0;
                    contributingFeatures.Add(FeatureID, Math.Min(FeatureFreq1[FeatureID].Count, FeatureFreq2[FeatureID].Count));
                }
                else
                {
                    double freq1 = 0;
                    if (FeatureFreq1.ContainsKey(FeatureID))
                        freq1 = FeatureFreq1[FeatureID].Count*FeatureFreq1[FeatureID].Weight;
                    double freq2 = 0;
                    if (FeatureFreq2.ContainsKey(FeatureID))
                        freq2 = FeatureFreq2[FeatureID].Count*FeatureFreq2[FeatureID].Weight;
                    if (useNGramSimilarity && ngramLen>0)
                    {
                        Dictionary<int, double> similarFeatures = featureSimilarityMatrix[FeatureID];
                        foreach (int similarFeatureID in similarFeatures.Keys)
                        {
                            if (FeatureFreq1.ContainsKey(similarFeatureID) &&
                                FeatureFreq1[similarFeatureID].Weight * FeatureFreq1[similarFeatureID].Count > freq1)
                                freq1 = FeatureFreq1[similarFeatureID].Weight*FeatureFreq1[similarFeatureID].Count;
                            if (FeatureFreq2.ContainsKey(similarFeatureID) &&
                                FeatureFreq2[similarFeatureID].Weight * FeatureFreq2[FeatureID].Count > freq2)
                                freq2 = FeatureFreq2[similarFeatureID].Weight*FeatureFreq2[FeatureID].Count;
                        }
                    }

                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double)contributingFeatures.Count * 2 /
                                 (FeatureFreq1.Count + FeatureFreq2.Count - contributingFeatures.Count);
            double euclideanDistance = Math.Sqrt(sumSq)*docLenScale;

            //Dictionary<int, double> contributingFeatureWeights = new Dictionary<int, double>();
            //foreach (int sharedFeatureID in contributingFeatures.Keys)
            //{
            //    if (FeatureWeights.ContainsKey(sharedFeatureID))
            //    {
            //        contributingFeatureWeights.Add(sharedFeatureID, FeatureWeights[sharedFeatureID]);
            //    }
            //    else
            //    {
            //        contributingFeatureWeights.Add(sharedFeatureID, 0);
            //    }
            //}
            double contributingFeatureScale =
                FeatureWeightCalculator.TaminoWeightedFeatureScale(
                    FeatureFreq1, FeatureFreq2,
                    FeatureWeights);

            return euclideanDistance*contributingFeatureScale;
        }

        public double CalculateDistance(
            Dictionary<int, FeatureFreq> FeatureFreq1, 
            Dictionary<int, FeatureFreq> FeatureFreq2,
            Dictionary<int, double> FeatureWeights,
            Dictionary<int, string> FeatureStemmings1,
            Dictionary<string,List<int>> FeatureStemmings2,
            ref Dictionary<int, int> contributingFeatures)
        {
            if (FeatureFreq1 == null || FeatureFreq2 == null || FeatureFreq1.Count == 0 || FeatureFreq2.Count == 0)
                return double.PositiveInfinity;

            List<int> unionFeatureIDs = new List<int>();
            foreach (int FeatureID1 in FeatureFreq1.Keys)
            {
                unionFeatureIDs.Add(FeatureID1);
            }
            foreach (int FeatureID2 in FeatureFreq2.Keys)
            {
                if (!unionFeatureIDs.Contains(FeatureID2))
                {
                    unionFeatureIDs.Add(FeatureID2);
                }
            }
            double sumSq = 0;

            foreach (int FeatureID in unionFeatureIDs)
            {
                if (FeatureFreq1.ContainsKey(FeatureID) && FeatureFreq2.ContainsKey(FeatureID))
                {
                    sumSq += 0;
                    contributingFeatures.Add(FeatureID, Math.Min(FeatureFreq1[FeatureID].Count, FeatureFreq2[FeatureID].Count));
                }
                else
                {
                    double freq1 = 0;
                    if (FeatureFreq1.ContainsKey(FeatureID))
                        freq1 = FeatureFreq1[FeatureID].Count * FeatureFreq1[FeatureID].Weight;
                    double freq2 = 0;
                    if (FeatureFreq2.ContainsKey(FeatureID))
                        freq2 = FeatureFreq2[FeatureID].Count * FeatureFreq2[FeatureID].Weight;
                    List<int> sharedFeatureIDs = FeatureStemmings2[FeatureStemmings1[FeatureID]];
                    List<int> sharedFeatureIDs1=new List<int>();
                    List<int> sharedFeatureIDs2 = new List<int>();
                    foreach(int FeatureID1 in FeatureFreq1.Keys)
                    {
                        if(sharedFeatureIDs.Contains(FeatureID1))
                        {
                            sharedFeatureIDs1.Add(FeatureID1);
                        }
                    }
                    foreach(int FeatureID2 in FeatureFreq2.Keys)
                    {
                        if(sharedFeatureIDs.Contains(FeatureID2))
                        {
                            sharedFeatureIDs2.Add(FeatureID2);
                        }
                    }
                    if (sharedFeatureIDs1.Count>0 && sharedFeatureIDs2.Count>0)
                    {
                        foreach (int similarFeatureID in sharedFeatureIDs1)
                        {
                            freq1 = FeatureFreq1[similarFeatureID].Weight * FeatureFreq1[similarFeatureID].Count;
                        }
                        foreach(int similarFeatureID in sharedFeatureIDs2)
                        {
                            freq2 = FeatureFreq2[similarFeatureID].Weight * FeatureFreq2[FeatureID].Count;
                        }
                    }

                    sumSq += Math.Pow(freq1 - freq2, 2);
                }
            }
            double docLenScale = (double)contributingFeatures.Count * 2 /
                                 (FeatureFreq1.Count + FeatureFreq2.Count - contributingFeatures.Count);
            double euclideanDistance = Math.Sqrt(sumSq) * docLenScale;

            //Dictionary<int, double> contributingFeatureWeights = new Dictionary<int, double>();
            //foreach (int sharedFeatureID in contributingFeatures.Keys)
            //{
            //    if (FeatureWeights.ContainsKey(sharedFeatureID))
            //    {
            //        contributingFeatureWeights.Add(sharedFeatureID, FeatureWeights[sharedFeatureID]);
            //    }
            //    else
            //    {
            //        contributingFeatureWeights.Add(sharedFeatureID, 0);
            //    }
            //}
            double contributingFeatureScale =
                FeatureWeightCalculator.TaminoWeightedFeatureScale(
                    FeatureFreq1, FeatureFreq2,
                    FeatureWeights);

            return euclideanDistance * contributingFeatureScale;
        }

        #endregion
    }
}
